package com.tpyule.tpclass.web.Controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tpyule.tpclass.entity.bean.FailResultBean;
import com.tpyule.tpclass.entity.bean.ResultBean;
import com.tpyule.tpclass.entity.bean.SuccessResultBean;
import com.tpyule.tpclass.entity.bo.UserDetailBo;
import com.tpyule.tpclass.entity.dto.CommonQueryDto;
import com.tpyule.tpclass.entity.pojo.Role;
import com.tpyule.tpclass.service.MenuService;
import com.tpyule.tpclass.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author zzz
 * @version 1.0
 * @date 2020/5/4 11:24
 */
@RequestMapping("/role")
@RestController
public class RoleController extends BaseController{

    @Autowired
    RoleService roleService;

    @Autowired
    MenuService menuService;


    @RequestMapping("/getSizeRole")
    @ResponseBody
    public void getSizeRole(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            int result = roleService.getSizeRole();
            resultBean = new SuccessResultBean(result,"成功");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
    @RequestMapping("/getListRole")
    @ResponseBody
    public void getListRole(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String queryData = request.getParameter("queryData");
            if(queryData!=null){
                CommonQueryDto dto = objectMapper.readValue(queryData,CommonQueryDto.class);
                if (dto!=null){
                    List<Role> roles = roleService.getListRole(dto);
                    if(roles!=null){
                        resultBean = new SuccessResultBean(roles, "成功");
                    }else {
                        resultBean = new FailResultBean("数据错误");
                    }
                } else {
                    resultBean = new FailResultBean("数据错误");
                }
            } else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
    @RequestMapping("/getAllRole")
    @ResponseBody
    public void getAllRole(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            List<Role> roles = roleService.getAllRole();
            resultBean = new SuccessResultBean(roles, "成功");
        } catch (Exception e) {
            resultBean = new FailResultBean();
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
    @RequestMapping("/updateRole")
    @ResponseBody
    public void updateRole(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String roleData = request.getParameter("roleData");
            String backPaths = request.getParameter("backPaths");
            String managePaths = request.getParameter("managePaths");
            if(roleData!=null){
                Role role = objectMapper.readValue(roleData,Role.class);
                roleService.updateRole(role,backPaths , managePaths);
                resultBean = new SuccessResultBean("成功");
            } else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            resultBean = new FailResultBean("系统异常");
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
    @RequestMapping("/add")
    @ResponseBody
    public void addRole(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String roleData = request.getParameter("roleData");
            String backPaths = request.getParameter("backPaths");
            String managePaths = request.getParameter("managePaths");
            if(roleData!=null){
                Role role = objectMapper.readValue(roleData,Role.class);
                roleService.addRole(role, backPaths, managePaths);
                resultBean = new SuccessResultBean("成功");
            } else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            resultBean = new FailResultBean("错误");
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
    @RequestMapping("/updateUserRole")
    @ResponseBody
    public void updateUserRole(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String userData = request.getParameter("userData");
            if(userData!=null){
                UserDetailBo bo = objectMapper.readValue(userData,UserDetailBo.class);
                roleService.addUpdateUserRole(bo);
                resultBean = new SuccessResultBean("成功");
            } else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (JsonProcessingException e) {
            resultBean = new FailResultBean("系统异常");
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
    @RequestMapping("/getUserRole")
    @ResponseBody
    public void getUserRole(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String userId = request.getParameter("userId");
            if(userId!=null){
                resultBean = new SuccessResultBean(roleService.getRoleByUserId(userId),"成功");
            } else {
                resultBean = new FailResultBean("数据错误");
            }
        } catch (Exception e) {
            resultBean = new FailResultBean("系统异常");
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }


}
